﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZealousTTS.Common.Extensions;
using ZealousTTS.Domain;
using ZealousTTS.Web.ViewModels;
using ZealousTTS.Web.Utility;
using AutoMapper;
using ZealousTTS.Web.ViewModels.UtilityModels;
using System.Data;


namespace ZealousTTS.Web.Areas.Private.Controllers
{
    public class ProjectController : BaseController
    {
		private ZealousTTSEntities _Context;
        
		#region Constructors
		public ProjectController()
		{
			_Context = new ZealousTTSEntities();
		}

		public ProjectController(UserViewModel currentUser)
			: base(currentUser)
        {
			_Context = new ZealousTTSEntities();
        }
		#endregion Constructors

		#region Action Methods
        public ActionResult Index()
        {
            return View();
        }
		/// <summary>
        /// Gets a Project from Database to edit
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
		public ActionResult Edit(int id = 0)
        {
            Project entity = id == 0 ? new Project() : _Context.Projects.FirstOrDefault(P => P.ID == id);            
            ProjectViewModel viewModel = Mapper.Map<Project, ProjectViewModel>(entity);             
            return View(viewModel);
        }

		/// <summary>
        /// Saves Project Information into the Database
        /// </summary>
        /// <param name="viewModel"></param>
        /// <returns></returns>
		[HttpPost]
		public ActionResult Edit(ProjectViewModel viewModel)
        {
			if (!ModelState.IsValid)
                return View(viewModel);

			Project entity = null; 
			if (viewModel.ID == 0)
            {
                entity = new Project();                
                _Context.AddToProjects(entity);
            }
            else
                entity = _Context.Projects.FirstOrDefault(P => P.ID == viewModel.ID);
           
            entity = Mapper.Map<ProjectViewModel, Project>(viewModel, entity);
            if (viewModel.ID == 0)
            {
                entity.Created = DateTime.Now;
                entity.IsDeleted = false;
            }
            _Context.SaveChanges();

            return RedirectToAction("Index", "Home");        
        }

        public ActionResult List()
        {
            UtilityDAO dao = new UtilityDAO();
            //*******   For Parameters Passing ********///////////
            //ZealousTTS.Domain.DbParameter[] parameters = new[]{
            //    new ZealousTTS.Domain.DbParameter(" ", LoggedInUser.Project.ID)
            //};
            DataSet ds = dao.GetDataSet(DbObject.SP.GET_PROJECT_STATUS, null, true);
            //DataTable dt = BuildDataTable();
            //DateTime start = DateTime.Now;
            ListConfig config = ZealousTTS.Web.ViewModels.UtilityModels.ListConfigBuilder.Build(
                @"Basic Info{ ID | Name | Description  }, 
                Project Info{IsActive | IsDeleted | Created}",
                ds.Tables[0], true, "/Private/Project/Edit/{0}",
                true);            


            ViewBag.ListConfig = config;
            return View(config);
           
        }       

		#endregion Action Methods
		
		#region Helper Methods

        #endregion			
    }
}
